import axios from 'axios'
import {Notification, MessageBox} from 'element-ui'

const baseURL = "/api";

axios.defaults.baseURL = baseURL;
axios.defaults.timeout = 50000 //设置接口响应时间
axios.defaults.withCredentials = true// 允许携带cookie
let msgFlag = false;

// http request 拦截器，通过这个，我们就可以把Cookie传到后台
axios.interceptors.request.use(
    config => {
        let token = sessionStorage.getItem('token')
        if (token === null) {
        } else {
            config.headers["token"] = token
        }

        return config
    },
    err => {
        return Promise.reject(err)
    }
)
// 路由响应拦截
// http response 拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.code != 0) {
            if (703 == response.data.code || 704 == response.data.code) {
                MessageBox({title: '错误', message: response.data.msg, type: 'error'})
            } else if (response.data.code > 7) {
                Notification({offset: 60, title: '错误', message: response.data.msg, type: 'error'});
            } else {
                if(response.data.code == 3){
                    sessionStorage.removeItem("token");
                    sessionStorage.removeItem("userInfo");
                    if(response.config.url != "/api/user/details/getUserInfo"){
                        window.location.href = '#/login'
                    }
                    if(!msgFlag) {
                        msgFlag = true;
                        Notification({
                            offset: 60, title: '未登陆', message: '请先登录', type: 'error', onClose: () => {
                                msgFlag = false
                            }
                        });

                    }
                }else  if(!msgFlag) {
                    msgFlag = true;
                    Notification({offset: 60, title: '错误', message: '发生错误', type: 'error',onClose:() =>{msgFlag = false}});
                }
            }
            return Promise.reject(response);
        }
        return response;
    },
    error => {
        console.log(error)
        if(!msgFlag){
            msgFlag = true;
            Notification({offset: 60, title: '错误', message: '服务器连接失败', type: 'error',onClose:() =>{msgFlag = false;}});
        }
        return Promise.reject(error.response)   // 返回接口返回的错误信息
    });


export default {
    axios,
    baseURL
}

/**
 * fetch 请求方法
 * @param url
 * @param params
 * @returns {Promise}
 */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios
            .get(url, {
                headers: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                params: params
            })
            .then(response => {
                resolve(response.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * post 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data).then(
            response => {
                // console.log(response.data.code)
                resolve(response.data)
            },
            err => {
                reject(err)
            }
        )
    })
}

/**
 * patch 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data).then(
            response => {
                resolve(response.data)
            },
            err => {
                reject(err)
            }
        )
    })
}

/**
 * put 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data).then(
            response => {
                resolve(response.data)
            },
            err => {
                reject(err)
            }
        )
    })
}

/**
 * upload 上传文件 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function upload(url, data) {
    return new Promise((resolve, reject) => {
        axios.post(url, data,{
            headers: { "Content-Type": "multipart/form-data" }
        }).then(
            response => {
                // console.log(response.data.code)
                resolve(response.data)
            },
            err => {
                reject(err)
            }
        )
    })
}